﻿@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="INV-AddInventory-EntryInventoryId">@ViewData["EntryInventoryId"]</label>
        <label id="INV-AddInventory-MaterialCode">@ViewData["MaterialCode"]</label>
        <label id="INV-AddInventory-MaterialName">@ViewData["MaterialName"]</label>
        <label id="INV-AddInventory-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">物料代码</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-AddInventory-Code" class="form-control font-rfs border-radius-noleft" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">物料名称</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-AddInventory-Name" class="form-control font-rfs border-radius-noleft" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">入库单编号</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-AddInventory-InventoryNumber" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">入库数量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="INV-AddInventory-EntryQuantity" class="form-control font-rfs border-radius-noleft" placeholder="必需是有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">入库单位*</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="INV-AddInventory-UnitsType" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <select id="INV-AddInventory-Units" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <button id="INV-AddInventory-AddUnits" onclick="AddUnits(this)" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">可领用数量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="INV-AddInventory-AvailableQuantity" class="form-control font-rfs border-radius-noleft" placeholder="必需是有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">领用单位*</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="INV-AddInventory-UnitsType1" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <select id="INV-AddInventory-Units1" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <button id="INV-AddInventory-AddUnits1" onclick="AddUnits(this)" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">存储位置</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="INV-AddInventory-Select-Location" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">存储条件</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="INV-AddInventory-Select-EquipmentType" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">有效期至</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-AddInventory-Vld" class="form-control font-rfs border-radius-noleft" style="width:45%" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">到期提醒</span>
            <input id="INV-AddInventory-Remind" class="form-control font-rfs border-radius-noleft" style="width:32%" placeholder="整数" onkeyup="value=value.replace(/[^\d]/g,'')" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单价</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-AddInventory-UnitPrice" class="form-control font-rfs border-radius-noleft" style="width:45%" value="0.00" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">总价</span>
            <input id="INV-AddInventory-TotalPrice" class="form-control font-rfs border-radius-noleft" style="width:32%" value="0.00" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-AddInventory-Note" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="text-right">
        <button id="INV-AddInventory-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="INV-AddInventory-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>
<div class="modal" id="INV-AddInventory-AddUnits-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="INV-AddInventory-AddUnits-modalTitle" class="pt-3 pl-3"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="ModalID" style="display:none">INV-AddInventory-AddUnits-Modal</label>
            </div>
            <div class="modal-body">
                <div id="INV-AddInventory-AddUnits-Embed"></div>
            </div>
        </div>
    </div>
</div>

<script>

    $(document).ready(function () {
        $("#INV-AddInventory-Code").val($("#INV-AddInventory-MaterialCode").html());
        $("#INV-AddInventory-Name").val($("#INV-AddInventory-MaterialName").html());
        LoadUnitType();
        GetLocationList('INV-AddInventory-Select-Location', '库存位置');

        $('#INV-AddInventory-Vld').datetimepicker({
            timepicker: false,
            format: "Y/m/d"
        });
        $.datetimepicker.setLocale('zh');

        if (($("#INV-AddInventory-EntryInventoryId").html()).length > 0) {
            LoadEntryInventory();
        }
    });

    function LoadUnitType() {
        $.post("\\Common\\GetUnitType", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#INV-AddInventory-UnitsType").append(new Option(data[i].type));
                $("#INV-AddInventory-UnitsType1").append(new Option(data[i].type));
            }
            $("#INV-AddInventory-UnitsType").prepend(new Option("", ""));
            $("#INV-AddInventory-UnitsType").val("");
            $("#INV-AddInventory-UnitsType1").prepend(new Option("", ""));
            $("#INV-AddInventory-UnitsType1").val("");
        });
    }

    $("#INV-AddInventory-UnitsType").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#INV-AddInventory-UnitsType").find("option:selected").text()
        }, function (data) {
            $("#INV-AddInventory-Units").empty();
            for (var i = 0; i < data.length; i++) {
                $("#INV-AddInventory-Units").append(new Option(data[i].code));
            }
        });
    });

    $("#INV-AddInventory-UnitsType1").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#INV-AddInventory-UnitsType1").find("option:selected").text()
        }, function (data) {
            $("#INV-AddInventory-Units1").empty();
            for (var i = 0; i < data.length; i++) {
                $("#INV-AddInventory-Units1").append(new Option(data[i].code));
            }
        });
    });

    function AddUnits(obj) {
        $("#INV-AddInventory-AddUnits-modalTitle").text("添加单位");

        $("#INV-AddInventory-AddUnits-Modal").modal({
            backdrop: 'static',
            keyboard: false
        });

        // $("#INV-AddInventory-AddUnits-Embed", parent.document.body).attr("src", "AddUnit");
        $.post("/Common/AddUnit", function (datas) {
            $("#INV-AddInventory-AddUnits-Embed").html(datas);
        });
    }

    function LoadEntryInventory() {
        $.post("GetInventory", {
            InventorId: $("#INV-AddInventory-EntryInventoryId").html()
        }, function (data) {
            $("#INV-AddInventory-InventoryNumber").val(data.inventoryNumber);
            $("#INV-AddInventory-EntryQuantity").val(data.entryQuantity);
            $("#INV-AddInventory-Units").append(new Option(data.entryUnit));
            $("#INV-AddInventory-AvailableQuantity").val(data.availableQuantity);
            $("#INV-AddInventory-Units1").append(new Option(data.availableUnit));
            $("#INV-AddInventory-Vld").val(data.vld == null ? null : data.vld.split('T')[0]);
            $("#INV-AddInventory-Remind").val(data.remind);
            $("#INV-AddInventory-UnitPrice").val(data.unitPrice);
            $("#INV-AddInventory-TotalPrice").val(data.totalPrice);
            $("#INV-AddInventory-Note").val(data.note);
            $("#INV-AddInventory-Select-Location").val(data.positionId);
        });
    }

    $("#INV-AddInventory-confirm").click(function () {
        if ($.trim($("#INV-AddInventory-EntryQuantity").val()).length == 0) {
            alert("库存数量不能为空！");
            $("#INV-AddInventory-EntryQuantity").addClass('error-style');
            return false;
        }
        if ($.trim($("#INV-AddInventory-Units").find("option:selected").text()).length == 0) {
            alert("库存单位必需选择！");
            $("#INV-AddInventory-Units").addClass('error-style');
            return false;
        }
        if ($.trim($("#INV-AddInventory-AvailableQuantity").val()).length == 0) {
            alert("可领用数量不能为空！");
            $("#INV-AddInventory-AvailableQuantity").addClass('error-style');
            return false;
        }
        if ($.trim($("#INV-AddInventory-Units1").find("option:selected").text()).length == 0) {
            alert("可领用单位必需选择！");
            $("#INV-AddInventory-Units1").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["MaterialCode"] = $.trim($("#INV-AddInventory-Code").val());
        jsonStr["MaterialName"] = $.trim($("#INV-AddInventory-Name").val());
        jsonStr["InventoryNumber"] = $.trim($("#INV-AddInventory-InventoryNumber").val());
        jsonStr["EntryQuantity"] = $.trim($("#INV-AddInventory-EntryQuantity").val());
        jsonStr["EntryUnit"] = $.trim($("#INV-AddInventory-Units").find("option:selected").text());
        jsonStr["AvailableQuantity"] = $.trim($("#INV-AddInventory-AvailableQuantity").val());
        jsonStr["AvailableUnit"] = $.trim($("#INV-AddInventory-Units1").find("option:selected").text());
        jsonStr["Vld"] = $('#INV-AddInventory-Vld').val();
        jsonStr["Remind"] = $('#INV-AddInventory-Remind').val();
        jsonStr["UnitPrice"] = $('#INV-AddInventory-UnitPrice').val();
        jsonStr["TotalPrice"] = $('#INV-AddInventory-TotalPrice').val();
        jsonStr["Note"] = $.trim($("#INV-AddInventory-Note").val());
        jsonStr["PositionId"] = $("#INV-AddInventory-Select-Location").find("option:selected").text();

        var EntryInventoryId = $("#INV-AddInventory-EntryInventoryId").html();
        if (EntryInventoryId == "") {
            $.post("postAddEntryInventory", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#INV-AddInventory-cancel").click();
                } else {
                    alert(data);
                }
            });
        } else {
            jsonStr["Id"] = $("#INV-AddInventory-EntryInventoryId").html();
            $.post("UpdateEntryInventory", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#INV-AddInventory-cancel").click();
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#INV-AddInventory-cancel").click(function () {
        var modalID = $("#INV-AddInventory-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
